<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Code_canvas extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Codemodel','m_code');
	}

	public function index($code_id){
		$code = $this->m_code->getCode($code_id);
		if(empty($code)) show_404();

		$data = parent::index();
		$data['attr'] = array(
			'code_id' => $code_id,
			'width' => $code['code_sprite_width'],
			'height' => $code['code_sprite_height'],
		);
		$this->load->view('code_canvas',$data);
	}

	public function addCodeSprite(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$info['code_sprite'] = $_FILES['code_sprite'];
		$total = count($info['code_sprite']['name']);
		$code = $this->m_code->getCode($info['code_id']);
		if(empty($code)){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		if(!$this->m_code->initCodeFolder($code['code_id'])){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		$existed_sprite = $this->m_code->getCodeSpriteList($info['code_id']);
		$existed_sprite = extractColumn($existed_sprite,'sprite_name');

		$file_info = array();
		for($i=0;$i<$total;$i++){
			if($info['code_sprite']['error'][$i] > 0) continue;
			if($info['code_sprite']['type'][$i] != 'image/png') continue;

			$file_pos = CODE_IMAGE_PATH.$code['code_id'].'/sprite/'.$info['code_sprite']['name'][$i];
			$file_flag = move_uploaded_file($info['code_sprite']['tmp_name'][$i],$file_pos);
			if(!$file_flag || !file_exists($file_pos)) continue;

			if(!in_array($info['code_sprite']['name'][$i],$existed_sprite)){
				$file_info[] = array(
					'code_id' => $info['code_id'],
					'sprite_name' => $info['code_sprite']['name'][$i],
					'sprite_path' => $code['code_id'].'/sprite/'.$info['code_sprite']['name'][$i],
					'sprite_x' => 500,
					'sprite_y' => 250,
					'sprite_status' => STATUS_PENDING,
					'sprite_time_create' => NOW,
				);
			}
		}

		$this->m_code->createCodeSpriteBatch($file_info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function getCodeSpriteList(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$code = $this->m_code->getCode($info['code_id']);
		$list = $this->m_code->getCodeSpriteList($info['code_id']);

		foreach ($list as $key => &$record) {
			$record['sprite_url'] = CODE_IMAGE_URL.$record['sprite_path'];
			$record['sprite_path'] = CODE_IMAGE_PATH.$record['sprite_path'];
			if(!file_exists($record['sprite_path'])) {
				unset($list[$key]);
				continue;
			}
			list($width, $height, $type, $attr)= getimagesize($record['sprite_path']);
			$record['width'] = $width;
			$record['height'] = $height;

			$record['sprite_path'] = trim($record['sprite_path'],'.');
		}
		$list = array_values($list);
		$count = count($list);


		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->setAddon(array(
			'width' => $code['code_sprite_width'],
			'height' => $code['code_sprite_height'],
		));
		$this->response->responseExit();
	}

	public function saveSprite(){
		$this->validate->setRule('code_id','integer');
		$this->validate->setRule('code_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('width','integer');
		$this->validate->setRule('width','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('height','integer');
		$this->validate->setRule('height','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('sprite','array');
		$this->validate->setRule('sprite','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(empty($info['sprite'])){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		$code = $this->m_code->getCode($info['code_id']);
		if(empty($code)){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		foreach($info['sprite'] as &$sprite){
			$sprite['sprite_id'] = intval($sprite['sprite_id']);
			$sprite['sprite_x'] = intval($sprite['sprite_x']);
			$sprite['sprite_y'] = intval($sprite['sprite_y']);
			$sprite['sprite_status'] = intval($sprite['sprite_status']);
		}

		$this->m_code->editCodeSpriteBatch(array_values($info['sprite']));
		$info['code_sprite_width'] = $info['width'];
		$info['code_sprite_height'] = $info['height'];
		unset($info['width']);
		unset($info['height']);
		unset($info['sprite']);
		$this->m_code->editCode($info);
		$this->m_code->generateSprite($code,$info['code_sprite_width'],$info['code_sprite_height']);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function deleteSprite(){
		$this->validate->setRule('sprite_id','integer');
		$this->validate->setRule('sprite_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		$sprite[] = array(
			'sprite_id' => $info['sprite_id'],
			'sprite_status' => STATUS_DELETE,
		);
		$this->m_code->editCodeSpriteBatch($sprite);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file canvas.php */
/* Location: ./application/controllers/canvas.php */